package com.sinosun.autotestmachine.tools.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 运行时记录器
 * @author HYW
 *
 */
public class RunningRecorder {
	private File file;
	private OutputStream os = null;
	private boolean stopFlag = false;
	public static RunningRecorder instance;
	
	public static RunningRecorder getRunningRecorder() {
		if (instance == null) {
			instance = new RunningRecorder();
		}
		return instance;
	}
	
	private RunningRecorder() {
		
	}
	
	public void initRunningRecorder(String path) {
		file = new File(path);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			os = new FileOutputStream(file, true);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		stopFlag = false;
	}
	
	/**
	 * 记录数据
	 * @param content 被记录的数据
	 */
	public void record(String content) {
		try {
			os.write(content.getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (stopFlag) {
			try {
				os.close();
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * 停止记录
	 */
	public void stopRecord() {
		stopFlag = true;
	}
}
